package com.scqkxx.gamebox.activity.home;

import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVQuery;
import com.avos.avoscloud.AVUser;
import com.avos.avoscloud.FindCallback;
import com.scqkxx.gamebox.R;
import com.scqkxx.gamebox.activity.activities.utils.ActivityUtil;
import com.scqkxx.gamebox.activity.activities.view.TitleView;
import com.scqkxx.gamebox.activity.group.GroupJoinActivity;
import com.scqkxx.gamebox.activity.home.adapter.HomeNewsAdapter;
import com.scqkxx.gamebox.bean.EvenBusBean;
import com.scqkxx.gamebox.databinding.FragmentActivitiesBinding;
import com.scqkxx.gamebox.utils.Utils;
import com.scqkxx.gamebox.widget.MySelfCallDialog;
import com.scqkzqtz.base.base.BaseFragment;

import org.greenrobot.eventbus.EventBus;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * 首页
 * Created by hef on 2018/8/10.
 */

public class HomeFragment extends BaseFragment<FragmentActivitiesBinding> implements View.OnClickListener {
    private List<AVObject> listNews = new ArrayList<>();
    private HomeNewsAdapter homeNewsAdapter;
    private MySelfCallDialog dialog;
    @Override
    public void loadData() {

    }

    @Override
    public int setLayoutId() {
        return R.layout.fragment_activities;
    }

    @Override
    public void init() {
        initView();
        initData();
    }

    private void initView() {
        AVUser avUser = AVUser.getCurrentUser();
        Log.e("TAG",avUser.toString());
        binding.username.setText(avUser.getUsername());
        binding.createTime.setText(new SimpleDateFormat("yyyy年MM月dd日").format(avUser.getCreatedAt()) +"加入nana");
        binding.titleView.getTv_right().setVisibility(View.GONE);
        binding.titleView.setOnTitleClickListener(mTitleType -> {
            if (TitleView.CLICK_BACK == mTitleType) {
                EventBus.getDefault().post(new EvenBusBean(0));
            }
        });
        //退出登录
        binding.tvConfirm.setOnClickListener(v -> EventBus.getDefault().post(new EvenBusBean(2)));

        try {
            binding.tvCacheNum.setText(Utils.getTotalCacheSize(_Activity));
        } catch (Exception e) {
            e.printStackTrace();
        }

        binding.tvClearCache.setOnClickListener(view -> {
            if(dialog == null){
                dialog = new MySelfCallDialog(_Activity);
            }
            dialog.show("确定清除？", new MySelfCallDialog.OnConfirmListener() {
                @Override
                public void onSure() {
                    Utils.clearAllCache(_Activity);
                    try {
                        binding.tvCacheNum.setText(Utils.getTotalCacheSize(_Activity));
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        });
    }

    private void initData() {
        if (homeNewsAdapter == null) homeNewsAdapter = new HomeNewsAdapter(R.layout.item_news, listNews);
        binding.mRecyclerView.setLayoutManager(new LinearLayoutManager(_Activity));
        binding.mRecyclerView.setAdapter(homeNewsAdapter);
        ViewGroup parentView = (ViewGroup) _Activity.getWindow().getDecorView();
        binding.mRecyclerView.setNestedScrollingEnabled(false);

        View view = getLayoutInflater().inflate(R.layout.seetting_end_iten, parentView ,false);
        //加入群组
        view.setOnClickListener(v -> ActivityUtil.switchTo(_Activity,GroupJoinActivity.class));
        homeNewsAdapter.addFooterView(view);
    }

    @Override
    public void onResume() {
        super.onResume();
        AVQuery<AVObject> query = AVQuery.getQuery("Group");
        query.findInBackground(new FindCallback<AVObject>() {
            @Override
            public void done(List<AVObject> avObjects, AVException e) {
                binding.smartRefreshLayout.finishRefresh(true);
                Log.i("Group",avObjects+"");
                if (e == null && avObjects != null && avObjects.size() > 0) {
                    listNews.clear();
                    listNews.addAll(avObjects);
                    homeNewsAdapter.setNewData(listNews);
                }
            }
        });
    }

    @Override
    public void onClick(View v) {

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
